package cn.lyq.card.ui.bank

import android.content.Context
import android.content.Intent
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.BankCardInfo
import cn.lyq.card.data.model.BankInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.global.BusTag
import cn.lyq.card.manager.PhoneCodeManager
import cn.lyq.card.manager.UserManager
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_bank_card_add.*


class BankCardAddActivity : BaseActivity() {

    private val mInfo = UserManager.bankCardInfo
    private val mModel by lazy {
        getVM(BankVM::class.java)
    }
    private var bankId = mInfo?.bankId ?: 0

    companion object {

        private const val KEY_INFO = "TYPE"

        @JvmStatic
        fun start(context: Context, bankCardInfo: BankCardInfo? = null) {
            val starter = Intent(context, BankCardAddActivity::class.java)
                .putExtra(KEY_INFO, bankCardInfo)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.act_bank_card_add
    }

    override fun initView() {
        super.initView()

        val isChange = mInfo != null
        setMyTitle(if (isChange) R.string.str_change_bank_card else R.string.str_add_bank_card)
        tvConfirm.setText(if (isChange) R.string.str_confirm_change else R.string.str_confirm_add)

        mInfo?.let {
            tvBank.text = it.name
            etBankCardNo.setText(it.cardNumber ?: "")
            etPhone.setText(it.phone ?: "")
        }
    }

    override fun initListener() {
        super.initListener()
        tvBank.setClick {
            startActivity(BankSelectActivity::class.java)
        }

        tvGetCode.setClick {
            PhoneCodeManager.sendMsgCode(4)
        }
        tvConfirm.setClick {
            if (bankId == 0) {
                ToastUtils.showShort(tvBank.hint)
                return@setClick
            }
            val bankCardNumber = etBankCardNo.text.trim().toString()
            if (bankCardNumber.isEmpty()) {
                ToastUtils.showShort(etBankCardNo.hint)
                return@setClick
            }
            val phone = etPhone.text.trim().toString()
            if (phone.isEmpty()) {
                ToastUtils.showShort(etPhone.hint)
                return@setClick
            }
            val code = etCode.text.trim().toString()
            if (code.isEmpty()) {
                ToastUtils.showShort(etCode.hint)
                return@setClick
            }
            mModel.editBankCard(bankCardNumber, bankId, phone, code).observe(this, Observer {
                when (it?.status) {
                    Status.SUCCESS -> {
                        BusUtils.post(BusTag.BANK_CARD_REFRESH)
                        ToastUtils.showShort("保存成功")
                        dismissLoading()
                        finish()
                    }
                    Status.LOADING -> showLoading()
                    Status.ERROR -> {
                        ToastUtils.showShort(it.message.getMsg())
                        dismissLoading()
                        finish()
                    }
                }
            })
        }
    }

    override fun initObserver() {
        super.initObserver()
        PhoneCodeManager.countDown.observe(this, PhoneCodeManager.observer(tvGetCode))
    }

    @BusUtils.Bus(tag = BusTag.BANK_SELECT, threadMode = BusUtils.ThreadMode.MAIN)
    fun bankSelect(info: BankInfo) {
        tvBank.text = info.name ?: ""
        bankId = info.id
    }

    override fun onDestroy() {
        super.onDestroy()
        PhoneCodeManager.reset()
    }
}